import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import LassoCV
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
#1
filename = r'E:\\images\boston_housing.csv'
df = pd.read_csv(filename)
sns.distplot(df['MEDV'], bins=30, kde=True)

#2
fig= plt.figure(figsize=(13, 9))
data_corr = df.corr()
sns.heatmap(data_corr, annot=True)

#4
y = df['MEDV']
x = df.drop('MEDV', axis = 1)

feat_names = x.columns

ss_X = MinMaxScaler()
ss_Y = MinMaxScaler()

X =ss_X.fit_transform(x)
Y = ss_Y.fit_transform(y.values.reshape(-1,1))


fe_data = pd.DataFrame(data=X, columns=feat_names, index=df.index)
fe_data['MEDV'] = y

fe_data.to_csv('boston_housing_result.csv', index=False)
# 图形出现在Notebook里面而不是窗口

# 读取做完特征工程后的数据
df = pd.read_csv('boston_housing_result.csv')

# 从原始数据中分离输入特征x和输出y
y = df['MEDV']
X = df.drop(['MEDV'], axis=1)

# 特征名称，用于后续显示权重系数对应的特征
feat_names = X.columns

# 将数据分割训练数据和测试数据


# 随机采样20%的数据构建测试样本，其余作为训练样本
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=0.2)
lr = LinearRegression()

# 2.用训练数据训练模型参数
lr.fit(X_train, y_train)

# 3.用训练好的模型对测试集进行预测
y_test_pred_lr = lr.predict(X_test)
y_train_pred_lr = lr.predict(X_train)

# 4.使用r2_score评价模型在测试集和训练集上的性能，并输出评价结果

#测试集
print("The r2 score of LinearRegression on test is", r2_score(y_test, y_test_pred_lr))
#训练集
print("The r2 score of LinearRegression on train is", r2_score(y_train, y_train_pred_lr))






#岭回归

# 1.设置超参数（正则参数）范围
# alphas = [0.01, 0.1, 1, 10, 100]

# 2.生成一个RidgeCV实例
# ridge = RidgeCV(alphas=alphas, store_cv_values=True)
ridge = RidgeCV()

# 3.训练模型
ridge.fit(X_train, y_train)

# 4.预测
y_test_pred_ridge = ridge.predict(X_test)
y_train_pred_ridge = ridge.predict(X_train)

# 5.使用r2_score评价模型在测试集和训练集上的性能，并输出评价结果
# from sklearn.metrics import r2_score
#测试集
print("The r2 score of RidgeCV on test is", r2_score(y_test, y_test_pred_ridge))
#训练集
print("The r2 score of RidgeCV on train is", r2_score(y_train, y_train_pred_ridge))



#Lasso
# 1.设置超参数（正则参数）范围
# alphas = [0.01, 0.1, 1, 10, 100]

# 2.生成一个RidgeCV实例
# lasso = LassoCV(alphas=alphas)
lasso = LassoCV()

# 3.训练模型
lasso.fit(X_train, y_train)

# 4.预测
y_test_pred_lasso = lasso.predict(X_test)
y_train_pred_lasso = lasso.predict(X_train)

# 5.使用r2_score评价模型在测试集和训练集上的性能，并输出评价结果
# from sklearn.metrics import r2_score
#测试集
print("The r2 score of LassoCV on test is", r2_score(y_test, y_test_pred_lasso))
#训练集
print("The r2 score of LassoCV on train is", r2_score(y_train, y_train_pred_lasso))